<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/stuWelcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>选课管理</el-breadcrumb-item>
      <el-breadcrumb-item>查询选课</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="10">
      <!--搜索框-->
      <el-col :span="6">
        <el-input placeholder="按课程号查询" v-model="searchId" class="input-with-select" @keyup.enter.native="searchData1">
          <el-button slot="append" icon="el-icon-search" @click="searchdata1"></el-button>
        </el-input>
      </el-col>
      <el-col :span="6" style="margin-left: 90px;margin-right: 90px;">
        <el-input placeholder="按课程名称查询" v-model="search" class="input-with-select" @keyup.enter.native="searchData2">
          <el-button slot="append" icon="el-icon-search" @click="searchdata"></el-button>
        </el-input>
      </el-col>
      <el-col :span="6">
        <el-input placeholder="按课程性质查询" v-model="searchNature" class="input-with-select"
                  @keyup.enter.native="searchData3">
          <el-button slot="append" icon="el-icon-search" @click="searchdata2"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <!--表单模块-->
    <el-table :data="selections"  border stripe height="300" style="margin-top: 30px;" highlight-current-row>
      <el-table-column type="index"  align="center"></el-table-column>
      <el-table-column prop="selId" label="选课号" align="center"></el-table-column>
      <el-table-column prop="couId" label="课程号"  align="center" width="120"></el-table-column>
      <el-table-column prop="stuNum" label="学号" width="130"  align="center"></el-table-column>
      <el-table-column prop="couName" label="课程名称" width="180"  align="center"></el-table-column>
      <el-table-column prop="couNature" label="课程性质" width="120"  align="center"></el-table-column>
      <el-table-column prop="couCredit" label="课程学分"  align="center"></el-table-column>
      <el-table-column prop="week" label="起始周"  align="center"></el-table-column>
      <el-table-column prop="location" label="上课地点"  align="center"></el-table-column>
    </el-table>
    <!--分页插件-->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curpage"
      :page-sizes="[2, 4, 8, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next"
      :total="total">
    </el-pagination>
  </el-card>
</template>

<script>
  import axios from 'axios';//axios
  export default {
    name:"stuSelectionList",
    //绑定数据
    data() {
      return {
        selections:[],//注意使用表格时，pop属性对应的内容等价于后端数据传输的字段(键)
        curpage:1,//当前页
        pagesize:4,//每页显示条数
        total:0,//总条数
        search:"",//模糊查询的内容
        searchId:"",
        searchNature:""
      }
    },
    //文档加载事件,当没有任何按钮去点击触发事件时，可以使用该函数在加载时，触发事件！
    mounted() {
      this.selectionList();
    },
    methods:{
      selectRow(row) { // 给操作行 高亮
        if (row) {
          this.$refs.table.setCurrentRow(row, true);  // 高亮某一行
        } else {
          this.$refs.table.setCurrentRow();  // 取消高亮
        }
      },
      //执行分页查询的函数
      selectionList(curpage = 1) {
        var selections=this;
        // 获取当前学号
        let stuNum = localStorage.getItem("stuNum");
        axios.get(
          "http://localhost:1313/selection/querySelectionByStuNumPage",
          {params:{current:selections.curpage, rows: selections.pagesize,search:stuNum}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.selections = res.data.data.selections;
          this.total = res.data.data.total;
        });
      },
      // 监听 pageSize 改变的事件
      handleSizeChange (newSize) {
        //   console.log(newSize)
        //  将监听接受到的每页显示多少条的数据 newSzie 赋值给 pagesize
        this.pagesize = newSize;
        //  修改完以后，重新发起请求获取一次数据
        this.selectionList();
      },
      // 监听 页码值  改变的事件
      handleCurrentChange (newPage) {
        //   console.log(newPage)
        //  将监听接受到的页码值的数据 newPage 赋值给 pagenum
        this.curpage = newPage;
        //  修改完以后，重新发起请求获取一次数据
        this.selectionList();
      },
      //模糊查询
      searchdata(){
        this.curpage = 1;
        //回调分页查询，增加分页查询的参数
        var selections=this;
        // 获取当前学号
        let stuNum = localStorage.getItem("stuNum");
        axios.get(
          "http://localhost:1313/selection/querySelectionByPage",
          {params:{current:selections.curpage, rows: selections.pagesize,stuNum:stuNum,search:selections.search}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.selections = res.data.data.selections;
          this.total = res.data.data.total;
        });
        this.search ="";
      },
      // 根据课程号完成查询
      selectionList1(){
        // 获取当前学号
        let stuNum = localStorage.getItem("stuNum");
        var selections=this;
        axios.get(
          "http://localhost:1313/selection/querySelectionByIdPage",
          {params:{current:selections.curpage, rows: selections.pagesize,stuNum:stuNum,search:selections.searchId}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.selections = res.data.data.selcourses;
          this.total = res.data.data.total;
        });
      },
      // 根据课程性质完成查询
      selectionList2(){
        // 获取当前学号
        let stuNum = localStorage.getItem("stuNum");
        var selections=this;
        axios.get(
          "http://localhost:1313/selection/querySelectionByCouNaturePage",
          {params:{current:selections.curpage, rows: selections.pagesize,stuNum:stuNum,search:selections.searchNature}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.selections = res.data.data.selections;
          this.total = res.data.data.total;
        });
      },
      searchdata1(){
        this.curpage = 1;
        this.selectionList1();
        this.searchId ="";
      },
      searchdata2(){
        this.curpage = 1;
        //回调分页查询，增加分页查询的参数
        this.selectionList2();
        this.searchNature ="";
      },
      // 按下回车键实现搜索
      searchData1(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata1();
        }
      },
      // 按下回车键实现搜索
      searchData2(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata();
        }
      },
      // 按下回车键实现搜索
      searchData3(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata2();
        }
      }
    }
  }

</script>

<style>
  /* 用来设置当前页面element全局table 选中某行时的背景色*/
  .el-table__body tr.current-row>td{
    background-color:  rgba(227, 189, 241, 0.76) !important;
    color: #fff;
  }
  .el-button--info.is-plain {
    color: #704141;
    background: #f4f4f5;
    border-color: #d3d4d6;
  }
  .el-button--mini {
    font-size: 14px;
    border-radius: 3px;
  }
  .el-breadcrumb{
    padding-bottom: 20px;
  }
  .el-pagination{
    padding-top: 20px;
  }
</style>
